package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;



import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.BuZou;

public class BuZouRowMapper implements RowMapper<BuZou>{
    private int type;

    public static final int BUZOU_AND_STR = 1;

    public BuZouRowMapper() {
        
    }

    public BuZouRowMapper(int type) {
        this.type = type;
    }

    @Override
    public BuZou mapRow(ResultSet rs, int rowNum) throws SQLException {
        BuZou bz = new BuZou();
        switch (type) {
            case BUZOU_AND_STR:
                bz = parseBuZou(new BuZou(), rs);
                bz.setBz_imgs(rs.getString("bz_imgs"));
                break;
            default:
                bz = parseBuZou(new BuZou(), rs);
        }
        return bz;
    }

    public static BuZou parseBuZou(BuZou bz, ResultSet rs)
        throws SQLException {
        return parseBuZou(bz, rs, null);
    }

    public static BuZou parseBuZou(BuZou bz, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            bz.setBz_id(rs.getLong(su + "bz_id"));
        } catch (SQLException e) {
            bz.setBz_id(null);
        }
        try {
            bz.setCp_id(rs.getLong(su + "cp_id"));
        } catch (SQLException e) {
            bz.setCp_id(null);
        }
        try {
            bz.setBz_msg(rs.getString(su + "bz_msg"));
        } catch (SQLException e) {
            bz.setBz_msg(null);
        }

      
        return bz;
    }

    public static Map<String, Object> getPar(Map<String, Object> par,
            BuZou bz) {
        par.put("bz_id", bz.getBz_id());
        par.put("cp_id", bz.getCp_id());
        par.put("bz_msg", bz.getBz_msg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            BuZou bz) {
        args.put("bz_id", bz.getBz_id());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final BuZou bz) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(bz),
                        new String[] { "bz_id" });
                parsePS(ps, bz);
                return ps;
            }

        };
    }

    private static String parseSQL(BuZou bz) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_buzou(");
        int count = 0;
        if (null != bz.getBz_id()) {
            sql.append("bz_id,");
            count++;
        }
        if (null != bz.getCp_id()) {
            sql.append("cp_id,");
            count++;
        }
        if (null != bz.getBz_msg()) {
            sql.append("bz_msg,");
            count++;
        }

        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, BuZou bz)
        throws SQLException {
        int count = 1;
        if (null != bz.getBz_id()) {
            ps.setLong(count++, bz.getBz_id());
        }
        if (null != bz.getCp_id()) {
            ps.setLong(count++, bz.getCp_id());
        }
        if (null != bz.getBz_msg()) {
            ps.setString(count++, bz.getBz_msg());
        }  


    }

}
